package com.open.capacity.task.controller;

import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.model.SysTask;
import com.open.capacity.common.util.PageUtil;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.task.model.TaskType;
import com.open.capacity.task.service.TaskService;

import com.open.capacity.task.utils.GetDate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Api(tags = "Task API")
@RequestMapping("/task")
public class TaskController {
    @Autowired
    TaskService taskService;

    @PostMapping("/findTasks")
    @ApiOperation(value ="查询任务（可条件查询）" )
    public PageResult<SysTask> findTasks(@RequestParam Map<String, Object> conditions){
        int count=taskService.findCounts(conditions);
        List<SysTask> tasks=taskService.findTasks(conditions);
        return PageResult.<SysTask>builder().data(tasks).count((long)count).code(0).build();
    }

    @PostMapping("/findTaskAreas")
    @ApiOperation(value = "查询任务范围（可条件查询）")
    public Result findTaskAreas(@RequestParam Map<String, Object> conditions){
        TaskType taskType=taskService.findTaskAreas(conditions);
        return null;
    }





    /**
     * 发布任务接口
     *
     * @param
     * @return
     */
    @PostMapping("/publishTask")
    @ApiOperation(value = "发布任务")
    public Result publishTask(@RequestParam Map<String, String> task) {

        //查询发布的任务种类，范围是否可用
        Map area = taskService.findTaskType(task.get("area"));
        Integer are = (Integer) area.get("status");
        Map type = taskService.findTaskLevel(task.get("type"));
        Integer typ = (Integer) type.get("status");

        if (are == 0 || typ == 0) {
            return Result.failed("暂时不能发布该类型任务");
        }
        //完善任务信息
        taskService.setInformation(task);

        //将任务存入任务表
        Integer count = taskService.publishTask(task);
        System.out.println(task);
        return Result.succeed("success");
    }

    /**
     * 更新任务接口(包含标题，内容，发布状态，积分，撤销)
     *
     * @param sysTask
     * @return
     */
    @PostMapping("/updateTask")
    @ApiOperation(value = "更新任务(包含标题，内容，发布状态，积分，撤销)")
    public Result updateTask(@RequestParam Map<String, String> sysTask) throws ControllerException {
        //获取更新时间
        String updtim = GetDate.get();
        sysTask.put("updtim", updtim);
        if (sysTask.get("integr") != null) {
            int point = Integer.parseInt(sysTask.get("integr"));
            point += 5;
            sysTask.put("integr", String.valueOf(point));
        }
        Integer count = taskService.updateTask(sysTask);
        return count == 1 ? Result.succeed("更新成功") : Result.failed("更新失败");
    }

    /**
     * 新增任务范围（前端，后台......）
     *
     * @param taskType
     * @return
     */
    @PostMapping("/addTaskType")
    @ApiOperation(value = "新增任务范围（前端，后台......）")
    public Result addTaskType(@RequestParam Map<String, String> taskType) {
        taskType.put("status", "1");
        taskType.put("cretim", GetDate.get());
        Integer count = taskService.addTaskType(taskType);
        return count == 1 ? Result.succeed("添加成功") : Result.failed("添加失败");
    }

    /**
     * 更新任务范围（前端，后台......）
     * @param taskType
     * @return
     */
    @PostMapping("/updateTaskType")
    @ApiOperation(value = "更新任务范围（前端，后台......）")
    public Result updateTaskType(@RequestParam Map<String, String> taskType){
        Integer count=taskService.updateTaskType(taskType);
        return count==1?Result.succeed("修改成功"):Result.failed("修改失败");
    }

    /**
     * 新增任务类型（普通，紧急......）
     *
     * @param type
     * @return
     */
    @PostMapping("/addTaskLevel")
    @ApiOperation(value = "新增任务类型：普通，紧急。。。")
    public Result addTaskLevel(@RequestParam Map<String, Object> type) {
        type.put("status", 1);
        type.put("cretim", GetDate.get());
        Integer count = taskService.addTaskLevel(type);
        return count == 1 ? Result.succeed("添加成功") : Result.failed("添加失败");
    }

    /**
     * 修改任务类型：普通，紧急。。。
     * @param type
     * @return
     */
    @PostMapping("/updateTaskLevel")
    @ApiOperation(value = "修改任务类型：普通，紧急。。。")
    public Result updateTaskLevel(@RequestParam Map<String, Object> type){
        Integer count=taskService.updateTaskLevel(type);
        return count==1?Result.succeed("修改成功"):Result.failed("修改失败");
    }




}
